#include "RobotVerifier.h"
#include "CanonicalConv.h"
#include <sys/stat.h>
#include "md5/md5.h"
#include "HTMLFetcher.h"
#include <string>
#include <fstream>
#include <iostream>

using namespace std;


bool FileExists(string strFilename) {
  struct stat stFileInfo;
  bool blnReturn;
  int intStat;

  // Attempt to get the file attributes
  intStat = stat(strFilename.c_str(),&stFileInfo);
  if(intStat == 0) {
    // We were able to get the file attributes
    // so the file obviously exists.
    blnReturn = true;
  } else {
    // We were not able to get the file attributes.
    // This may mean that we don't have permission to
    // access the folder which contains this file. If you
    // need to do that level of checking, lookup the
    // return values of stat which will give you
    // more details on why stat failed.
    blnReturn = false;
  }
  
  return(blnReturn);
}


RobotVerifier::RobotVerifier()
{
}

RobotVerifier::~RobotVerifier()
{
}

bool RobotVerifier::analyseRobotsTXT(string baseUrl, std::ifstream* file)
{

	CanonicalConv canConv;
	string desiredPath = canConv.getCanonicalPath(baseUrl).append("/");
	//cout << " Desired Path: " << desiredPath << endl;
	bool inloop=false;
	int linhaNum=0;
	while(!file->eof())
	{
		string line;
		//cout << "Linha: " << linhaNum << endl;
		getline(*file,line);
		
		//line = line.substr(0,line.length()-1);
		//cout << "Li a Linha: " << line << endl;
		//cout << "Substr test: "<< line.substr(0,11).compare("User-agent:") << endl;
		if(line.find("User-agent:",0)!=string::npos && inloop == true)
		{
			//cout << " Line starts with: User-agent" << endl;
			inloop = false;
		}
		//cout << "Li a Linha: " << line << endl;
		if(line.find("User-agent: *")!=string::npos)
		{
			//cout <<" Line starts with: User-agent: *" << endl;
			inloop = true;
		}
		if(inloop == true)
		{
			string linePath="";
			if(line.compare("Allow: /")==0)
			{
				//cout << "Found an: Allow: /, returning true" << endl;
				return true;
			}
				else if(line.compare("Disallow: /")==0)
				{
					//cout << "Found an: Disallow: /, returning false" << endl;
					return false;					
				}
				
			else if(line.length()>=6){
			if(line.substr(0,6).compare("Allow:")==0)
			{
				//cout << "Found an: Allow: " << endl;
				if(line.length() > 7)
					linePath = line.substr(7);
				linePath = linePath.append("/");
				//cout << "linePath is: " << linePath << endl;
				if(desiredPath.find(linePath)== 0)
				{
					//cout << " This allow is for the desiredPath, so ret. true" << endl;
					return true;
				}
			}
			}
			else
			{
				if(line.length()>=9)
				{
				if(line.substr(0,9).compare("Disallow:")==0)
				{
					//cout << "Found an: Disallow: " << endl;
					if(line.length() > 10)
						linePath = line.substr(10);
					linePath = linePath.append("/");
					//cout << "linePath is: " << linePath << endl;
					if(desiredPath.find(linePath)== 0)
					{
						//cout << " This disallow is for the desiredPath, so ret. false" << endl;
						return false;
					}
				}
				}
			}
		}
		linhaNum++;
		
	}
	return true;
}

bool RobotVerifier::analyseRobotsTXT(string baseUrl, string domain)
{
	char* temp = const_cast<char*>(domain.c_str()) ;
	string fileName = MD5String(temp);
	fileName = fileName.append(".rob");
	std::ifstream file;
	string path = fileName.substr(0,3).append("/").append(fileName);
	//cout << "PATHHHHHHHHHHH: " << path << endl;
	file.open(path.c_str(),ifstream::in);
	if(file.is_open())
	{
		//cout << "Could open file" <<endl;
		bool resp = analyseRobotsTXT(baseUrl,&file);
		file.close();
		return resp;
	}
	else
	{
		//cout << " Couldn't open file" << endl;
		return true;
	}
}

int RobotVerifier::fetchRobotsTXT(string baseDomain)
{
	HTMLFetcher fetcher;
	char* temp = const_cast<char*>(baseDomain.c_str()) ;
	string file = MD5String(temp);
	file = file.append(".rob");
	string path = file.substr(0,3).append("/").append(file);
	//cout << "PATHHHHHHHHHH " << path << endl;
	if(!FileExists(path))
	{
		int res = fetcher.fetchPage(baseDomain.append("/robots.txt"),file);
		return res;
	}
	return -1;
}
